class Tag < ActiveRecord::Base
  has_and_belongs_to_many :documents

  validates :label, :presence   => true,
                    :uniqueness => {:case_sensitive => false},
                    :length     => {:within => 2..60}

  default_scope order('label ASC')
  scope :by_document_type, lambda { |type| joins(:documents).where('documents.source_type' => type.to_s.capitalize).select("DISTINCT label")}
end
